package com.xinli.business.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.xinli.business.domain.*;
import com.xinli.business.service.*;
import com.xinli.common.annotation.RepeatSubmit;
import com.xinli.common.core.controller.BaseController;
import com.xinli.common.core.domain.AjaxResult;
import com.xinli.common.core.page.TableDataInfo;
import com.xinli.common.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页
 */
@RestController
@RequestMapping("/webApiIndex")
public class TWebApiIndexController extends BaseController {


    @Autowired
    private ITConfigService tConfigService;


    @Autowired
    private ITGuideService tGuideService;


    @Autowired
    private ITTwxxService tTwxxService;

    @Autowired
    private ITOrderService tOrderService;

    @Autowired
    private YzCodeUtils yzCodeUtils;

    @Autowired
    private IFtMemberService memberService;

    @Autowired
    private ITZxsFollowService tZxsFollowService;

    @Autowired
    private ITQuestionFollowService questionFollowService;


    @Autowired
    private ITQuestionService tQuestionService;


    @Autowired
    private ILfPostService postService;


    @Autowired
    private ILfTopicService topicService;

    @Autowired
    private ILfCommentService lfCommentService;


    /**
     * 获取一条专业问答
     */
    @GetMapping("/nologin/getQuestionList")
    public AjaxResult getQuestionList() {
        LfPost lfPost = new LfPost();
        List<Long> topicIds = topicService.getTopicIds(1);
        lfPost.setTopicIdArray(topicIds);
        PageUtils.startPage(1, 1);
        List<LfPost> list = postService.selectLfPostList(lfPost);
        for (int i = 0; i < list.size(); i++) {
            postService.setViewAndReplyCountAndReList(list.get(i),"");
        }
        if (!CollectionUtil.isEmpty(list)) {
            return AjaxResult.success(list.get(0));
        } else {
            return AjaxResult.success();
        }
    }

    /**
     * 专业咨询师
     */
    @GetMapping("/nologin/getZxsList")
    public AjaxResult getZxsList(FtMember vo) {
        PageUtils.startPage(1, 1);
        List<FtMember> ftMembers = memberService.selectFtMemberListApp(vo);
        return AjaxResult.success(ftMembers);
    }


    /**
     * 获取一条科普文章
     */
    @GetMapping("/nologin/getPost")
    public AjaxResult getPost() {
        LfPost lfPost = new LfPost();
        PageUtils.startPage(1, 1);
        List<Long> topicIds = topicService.getTopicIds(2);
        lfPost.setTopicIdArray(topicIds);
        List<LfPost> posts = postService.selectLfPostList(lfPost);
        if (!CollectionUtil.isEmpty(posts)) {
            LfPost lfPost1 = posts.get(0);
//            Long id = lfPost1.getId();
//            LfComment  reply = new LfComment();
//            reply.setPostId(id);
//            reply.setPid(Long.valueOf(0));
//            List<LfComment> lfComments = lfCommentService.selectLfCommentList(reply);
//            lfPost1.setCommentList(lfComments);
            return AjaxResult.success(lfPost1);
        } else {
            return AjaxResult.success();
        }

    }


    /**
     * 发布问题
     */
    @PostMapping("/addPost")
    @RepeatSubmit
    public AjaxResult addPost(@RequestBody LfPost post) {
        post.setUid(SecurityUtils.getUserId());
        post.setCreateTime(DateUtils.getNowDate());
        post.setCreateBy(SecurityUtils.getUserIdStr());
        postService.insertLfPost(post);
        return AjaxResult.success();
    }


}
